#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QHBoxLayout>
#include <QGraphicsLinearLayout>
#include <QGraphicsWidget>





MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QPainterPath* path = new QPainterPath;
    path->addText(100,100,QFont("Arial"),"Scene.Point(100,100)");
    path->moveTo(100,100);
    path->lineTo(200,200);


    scene = new QGraphicsScene;
    view = new QGraphicsView;
    view->setScene(scene);

    // 设置view
    view->setRenderHint(QPainter::Antialiasing);
    view->fitInView(scene->sceneRect(), Qt::KeepAspectRatioByExpanding);
    view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
    view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view->setCacheMode(QGraphicsView::CacheNone);


    QHBoxLayout* layout = new QHBoxLayout;
    layout->setContentsMargins(0,0,0,0);
    layout->addWidget(view);
    ui->centralwidget->setLayout(layout);

    widget = new OscilloscopeWidget;
    widget->setCurvePath(path);
    widget->setColor(Qt::red,Qt::gray);
    widget->setCurveName("name1", QFont("Arial", 5));
    widget->setLabelWidth(100);


    // widget1 = new OscilloscopeWidget;
    // widget1->setCurvePath(path);
    // widget1->setColor(Qt::blue, Qt::gray);
    // widget1->setCurveName("name2", QFont("Arial", 10));
    // widget1->setLabelWidth(100);

    scene->addItem(widget);
    // scene->addItem(widget1);
    // QGraphicsLinearLayout* sceneLayout = new QGraphicsLinearLayout(Qt::Vertical);
    // sceneLayout->addItem(widget);
    // sceneLayout->addItem(widget1);

    // sceneLayout->setContentsMargins(0,0,0,0);
    // wd = new QGraphicsWidget;
    // wd->setLayout(sceneLayout);
    // view->centerOn(scene->sceneRect().topLeft());
    // scene->addItem(wd);

    view->show();

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)

    scene->setSceneRect(view->viewport()->geometry().left() - view->viewport()->width() / 2,
                        view->viewport()->geometry().top() - view->viewport()->height() / 2,
                        view->viewport()->geometry().width(),
                        view->viewport()->geometry().height()
                        );



    // wd->resize(view->viewport()->size().width(), 100);
    // wd->setPos(scene->sceneRect().topLeft());
    // qDebug()<< wd->size() << widget->size();
    widget->setPos(view->mapFromScene( widget->pos()));
    qDebug()<< "widget:" << view->mapFromScene( widget->pos());

}

void MainWindow::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event)

    scene->setSceneRect(view->viewport()->geometry().left() - view->viewport()->width() / 2,
                        view->viewport()->geometry().top() - view->viewport()->height() / 2,
                        view->viewport()->geometry().width(),
                        view->viewport()->geometry().height()
                        );


    // wd->resize(view->viewport()->size().width(), 100);
    // wd->setPos(scene->sceneRect().topLeft());
    // qDebug()<<wd->size() << widget->size();
    qDebug()<< "widget:" << view->mapFromScene( widget->pos());
    widget->setPos(view->mapFromScene( widget->pos()));
}
